package com.ws.shop.util;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

public class RequestUtil {

	public static int getIntParameter(HttpServletRequest request,String paramName,int defaultValue){
		String value = request.getParameter(paramName);
		if(StringUtil.isEmpty(value)){
			return defaultValue;
		}else{
			try{
				return Integer.parseInt(value);
			}catch(Exception e){
				return defaultValue;
			}
		}
	}
	
	public static long getLongParameter(HttpServletRequest request,String paramName,long defaultValue){
		String value = request.getParameter(paramName);
		if(StringUtil.isEmpty(value)){
			return defaultValue;
		}else{
			try{
				return Long.parseLong(value);
			}catch(Exception e){
				return defaultValue;
			}
		}
	}
	
	public static double getDoubleParameter(HttpServletRequest request,String paramName,double defaultValue){
		String value = request.getParameter(paramName);
		if(StringUtil.isEmpty(value)){
			return defaultValue;
		}else{
			try{
				return Double.parseDouble(value);
			}catch(Exception e){
				return defaultValue;
			}
		}
	}
	
	public static String getStringParameter(HttpServletRequest request,String paramName,String defaultValue){
		String value = request.getParameter(paramName);
		if(StringUtil.isEmpty(value)){
			return defaultValue;
		}else{
			return value.trim();
		}
	}

	/**
	 * 通过get方式传递的参数
	 * @param request
	 * @param string
	 * @param object
	 * @return
	 */
	public static String getStringParameterByGet(HttpServletRequest request,String paramName, String defaultValue) {
		String value = request.getParameter(paramName);
		if(StringUtil.isEmpty(value)){
			return defaultValue;
		}else{
			try {
				return new String(value.trim().getBytes("iso-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				return defaultValue;
			}
		}
	}

	public static float getFloatParameter(HttpServletRequest request,
			String paramName, float defaultValue) {
		String value = request.getParameter(paramName);
		if(StringUtil.isEmpty(value)){
			return defaultValue;
		}else{
			try{
				return Float.parseFloat(value);
			}catch(Exception e){
				return defaultValue;
			}
		}
	}
	
   
}
